﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace BattleThis
{
	public class PersistenceAttribute : ActionFilterAttribute
	{

		#region Variablen
		#endregion

		#region Properties
		#endregion

		#region Methoden

		public override void OnActionExecuting( ActionExecutingContext filterContext )
		{
			if( filterContext.IsChildAction )
				return;

			if( PersistenceContext.Current.UnitOfWork == null )
			{
				throw new InvalidOperationException("Unit of Work is not available.");
			}
		}

		public override void OnActionExecuted( ActionExecutedContext filterContext )
		{
			if( filterContext.IsChildAction )
				return;

			if( filterContext.Exception == null )
			{
				PersistenceContext.Current.UnitOfWork.Commit();
			}

			PersistenceContext.Current.Clear();

			base.OnActionExecuted( filterContext );
		}

		#endregion

	}
}